package com.wanghua.spring.action;

import com.opensymphony.xwork2.ActionSupport;
import com.wanghua.spring.listeners.PersonInfo;
import com.wanghua.spring.util.StringUtil;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.sql.*;
import java.util.Map;

@Scope("prototype")
@Controller("dateTimeConvertorAction")
public class DateTimeConvertorAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,SessionAware,ServletContextAware {
    //下面三个时间，用于测试自定义的转换器 DateTimeConvertor。
    private java.sql.Date sqlDate;
    private java.sql.Time sqlTime;
    private java.util.Date utilDate;

    //下面 Servlet 变量，利用相关 Aware(感知) 接口，实现资源感知，即实现注射。Aware 接口其实是一种拦截器，拦截器代码会在执行 Action 之前执行，将相关 Servlet 对象设置进来。

    private HttpServletRequest request;
    private HttpServletResponse response;
    private ServletContext context;
    private Map session;  //该 Map 代表 Session 内存储的各种数据

    @Override
    public String execute() throws Exception {
        //虽然是使用静态方法获取的 request、response,仍然能保证获取的是当前用户当前线程的,因为 struts2 利用 ThreadLocal 来维护当前线程的 request、response 等数据。
        //HttpServletRequest request = ServletActionContext.getRequest();
        //HttpServletResponse response = ServletActionContext.getResponse();
        //ServletContext context = ServletActionContext.getServletContext();
        //HttpSession session = request.getSession();   //用法:session.getAttribute("account");session.setAttribute("account", "wang.h");

        //Session用法（将浏览器多刷新几下可看到 account 在叠加）
        Object account = session.get("account");
        if (StringUtil.isNull(account))
            session.put("account", "wang.h");
        else
            session.put("account", account + "_" + account);
        File uploadFile = new File(context.getRealPath("/upload"));  //获取 upload 文件夹

        return INPUT;
    }

    public String convert() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        HttpSession session = request.getSession();

        return SUCCESS;
    }

    public Date getSqlDate() {
        return sqlDate;
    }

    public void setSqlDate(Date sqlDate) {
        this.sqlDate = sqlDate;
    }

    public Time getSqlTime() {
        return sqlTime;
    }

    public void setSqlTime(Time sqlTime) {
        this.sqlTime = sqlTime;
    }

    public java.util.Date getUtilDate() {
        return utilDate;
    }

    public void setUtilDate(java.util.Date utilDate) {
        this.utilDate = utilDate;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.context = servletContext;
    }

    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }

    @Override
    public void setSession(Map<String, Object> map) {
        this.session = map;
    }
}
